// pages/pay/pay.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    thingId: '',
    count: 0,
    price: 0,
    amount: 0,
    orderTime: 0,
    countdown: '',
    receiverAddress: '',
    receiverName: '',
    receiverPhone: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      orderId: options.orderId,
      thingId: options.thingId,
      count: parseInt(options.count),
      price: parseFloat(options.price),
      amount: parseFloat(options.amount),
      orderTime: parseInt(options.orderTime)
    });
    this.startCountdown();
  },

  startCountdown() {
    const endTime = this.data.orderTime + 12 * 60 * 60 * 1000;
    const interval = setInterval(() => {
      const now = Date.now();
      const diff = endTime - now;
      if (diff <= 0) {
        clearInterval(interval);
        this.setData({
          countdown: '支付已超时'
        });
        // 可以在这里添加超时后的处理逻辑，如自动取消订单
      } else {
        const hours = Math.floor(diff / (1000 * 60 * 60));
        const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
        const seconds = Math.floor((diff % (1000 * 60)) / 1000);
        this.setData({
          countdown: `${hours}小时${minutes}分${seconds}秒`
        });
      }
    }, 1000);
  },

  goBack() {
    wx.navigateBack({
      delta: 1 // 返回上一级页面
    });
  },

  onReceiverAddressInput(e) {
    this.setData({
      receiverAddress: e.detail.value
    });
  },

  onReceiverNameInput(e) {
    this.setData({
      receiverName: e.detail.value
    });
  },

  onReceiverPhoneInput(e) {
    this.setData({
      receiverPhone: e.detail.value
    });
  },

  // 支付订单方法定义
  confirmPayment() {
    const app = getApp();
    const token = app.globalData.token;
    const {
      orderId,
      receiverAddress,
      receiverName,
      receiverPhone
    } = this.data;
    // 获取当前时间作为支付时间
    const payTime = new Date().toISOString();

    wx.request({
      url: 'http://127.0.0.1:9100/api/order/update',
      method: 'POST',
      header: {
        'TOKEN': token,
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        id: orderId,
        status: '2',
        receiverAddress,
        receiverName,
        receiverPhone,
        payTime // 添加支付时间
      },
      success(res) {
        if (res.data.code === 200) {
          wx.showToast({
            title: '支付成功',
            icon: 'success'
          });
          // wx.navigateBack({
          //   delta: 2 // 返回订单详情页面
          // });
          // 修改跳转逻辑，跳转到 thing.wxml 对应的页面
          wx.navigateTo({
            url: '/pages/thing/thing'
          });
        } else {
          wx.showToast({
            title: '支付失败',
            icon: 'none'
          });
        }
      },
      fail() {
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        });
      }
    });
  },

  // 取消订单方法定义
  cancelOrder() {
    const app = getApp();
    const token = app.globalData.token;
    const {
      orderId
    } = this.data;
    wx.request({
      url: 'http://127.0.0.1:9100/api/order/cancelUserOrder',
      method: 'POST',
      header: {
        'TOKEN': token,
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        id: orderId
      },
      success(res) {
        if (res.data.code === 200) {
          wx.showToast({
            title: '订单取消成功',
            icon: 'success'
          });
          wx.navigateBack({
            delta: 2 // 返回订单详情页面
          });
        } else {
          wx.showToast({
            title: '订单取消失败',
            icon: 'none'
          });
        }
      },
      fail() {
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})